<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ep="urn:ep"
    xmlns:str="http://exslt.org/strings"
    extension-element-prefixes="str"
    version="1.0">

    <xsl:output method="html" />
    <xsl:include href="ep_config.xsl" />
    <xsl:param name="epc_targets"/>
    <xsl:param name="standalone"/>

    <xsl:template match="/action_metadata">
        <xsl:if test="$standalone">
            <head>
<!-- disabled SVG check until 2.0
        <script language="JavaScript" src="{$html_href}/static/js/svgcheck.js"></script>
        <script language="VBScript" src="{$html_href}/static/js/svgcheck.vbs"></script>
        <script language="JavaScript">checkAndGetSVGViewer();</script>
-->

                <script type="text/javascript" src="{$html_href}/static/js/ep_ui.js"></script>
                <script type="text/javascript" src="{$html_href}/static/js/ep_xml.js"></script>
    
                <script type="text/javascript">
                    var cgi_href="<xsl:value-of select="$cgi_href"/>";
                    var html_href="<xsl:value-of select="$html_href"/>";
                </script>
            </head>
        </xsl:if>

    <script type="text/javascript">//<![CDATA[
function seeobj ( obj ) {
  var s = "";

  for ( p in obj ) {
    if ( typeof ( obj[p] ) == "function" ) continue;
    s += p + ": " + obj[p] + "\n";
  }

  return s;
}

function ci(vg,Pf,yh) {
  this.type=vg;
  this.version=Pf;
  this.os=yh;
}

var m = new ci(0,0,null);
var Eb = navigator.userAgent.toLowerCase();
if ( Eb.indexOf ("msie") != -1 && document.all ) {
  m.type=1;
  if ( Eb.indexOf("msie 5") ) m.version=5;
} else if ( Eb.indexOf("safari") != -1 ) {
  m.type=3;
  if ( Eb.indexOf ("safari/125") !=-1 ) m.version=1;
} else if ( Eb.indexOf ("mozilla") != -1 ) {
  m.type=2;
} if ( Eb.indexOf ( "x11;" ) !=-1 ) {
  m.os=1;
}

function Xd() {};

Xd.create = function () {
  if ( m.type==1 ) {
    var Ud = m.version == 5 ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
    return new ActiveXObject(Ud);
  } else {
    return new XMLHttpRequest();
  }
}

function Y ( Vc ) {
  if ( m.type == 1 ) {
    this.stylesheet = Vc;
  } else if ( m.type != 3 ) {
    this.processor=new XSLTProcessor();
    this.processor.importStylesheet(Vc);
  }
}

Y.cache_=new Object();

Y.create=function(Vc) {
  return new Y(Vc);
}
 
Y.getCached=function(lb) {
  return Y.cache_[lb];
}

Y.cache = function (lb, Nf) {
  Y.cache_[lb]=Nf;
}

Y.prototype.transformToHTML=function(qc,vb,params) {
  if ( this.stylesheet ) {
    for ( param in params ) {
        // hackish... :)
      var p = this.stylesheet.selectSingleNode ( '/xsl:stylesheet/xsl:param[@name="' + param + '"]' );
      p.text = params[param];
    }

    vb.innerHTML=qc.transformNode(this.stylesheet);
  } else {
    for ( param in params ) {
      this.processor.setParameter ( null, param, params[param] );
    }
    var Uh=this.processor.transformToFragment(qc,window.document);
    vb.innerHTML="";
    vb.appendChild(Uh);
  }
}

// kc - stylesheet URL
// qc - xml to transform
// vb - where in document to place the results
// Nc - function to call after transform
// params - array of parameters
Y.asynchronousTransform=function(qc,vb,kc,Nc,params) {
  if(m.type==3) return;

  var cc=Y.getCached(kc);

  if (cc) {
    cc.transformToHTML(qc,vb,params);
    if(Nc) Nc();
    return;
  }

  var sa = Xd.create();
  sa.open ( "GET", kc, true );

  sa.onreadystatechange = function () {
    if ( sa.readyState==4 ) {
      try {
	var Db = sa.responseXML;
	var cc = Y.create ( Db );
	Y.cache ( kc, cc );
	cc.transformToHTML ( qc, vb, params );
	if(Nc) Nc();
      } catch(b) {}
    }
  };

  sa.send(null);
}

function render_table ( table_div_id, params ) {
  params.table_div_id = table_div_id;
  Y.asynchronousTransform ( document.getElementById(table_div_id).xml,document.getElementById(table_div_id), html_href + '/static/xsl/format_table.xsl', null , params );
}

function load_table ( table_div_id, meta_id ) {
    var sa = Xd.create();
    sa.open ( "GET", cgi_href + "/get_xml.pl?meta_id=" + meta_id, true );

    sa.onreadystatechange = function () {
    if ( sa.readyState==4 ) {
      try {
        document.getElementById ( table_div_id ).xml = sa.responseXML;
           render_table(table_div_id, {} );
         } catch(b) {}
        }
      };

      sa.send(null);
}
      //]]></script>

    <xsl:call-template name="epc_target_links" />

    <h1>Results</h1>
    <xsl:apply-templates select="meta[@type!='input'][@label!=''][@type!='text/plain']"/>
    <xsl:apply-templates select="meta[@type!='input'][@label!=''][@type='text/plain']"/>
    <xsl:apply-templates select="meta[@name='html']"/>

    <h1 onmouseover="this.style.cursor='pointer'" onclick="document.getElementById('other_action_metadata').style.display=(document.getElementById('other_action_metadata').style.display=='none'?'':'none')">Show other metadata >></h1>
    <div id="other_action_metadata" style="display:none">
      <xsl:apply-templates select="meta[@type!='input'][@name!='html'][@label='']"/>
      <xsl:apply-templates select="meta[@type='input']"/>
    </div>

    <xsl:call-template name="epc_target_links"/>
  </xsl:template>

  <xsl:template match="meta[@type='image/png']">
    <h2><xsl:value-of select="@label"/></h2>
    <xsl:variable name="image_map_meta_name" select="concat(@name, '_image_map')" />
    <xsl:variable name="png_image_map_location" select="concat('file://', ep:convert_url_to_path(string(../meta[@name=$image_map_meta_name])))"/>
    <xsl:choose>
        <xsl:when test="$png_image_map_location != 'file://'">
            <img style="align:center; border: 1px solid #bfbfbf" src="{text()}" usemap="#{@name}"/>
            <map name="{@name}"><xsl:copy-of select="document($png_image_map_location)/map/*"/></map>
        </xsl:when>
        <xsl:otherwise>
            <img style="align:center; border: 1px solid #bfbfbf" src="{text()}"/>
        </xsl:otherwise>
    </xsl:choose>
    <br/><br/>
  </xsl:template>

  <xsl:template match="meta[@type='image/svg+xml']">
    <h2><xsl:value-of select="@label"/></h2>
    <xsl:variable name="svg_width" select="concat(@name,'_width')"/>
    <xsl:variable name="svg_height" select="concat(@name,'_height')"/>
    
    <div style="margin-left:30px;margin-right:30px">
      <embed wmode="opaque" src="{concat($cgi_href,'/get_xml.pl?meta_id=',@id)}" name="{@name}" id="{@name}"
        type="image/svg+xml" pluginspage="http://www.adobe.com/svg/viewer/install/"
        width="{../meta[@name=$svg_width]/text()}" height="{../meta[@name=$svg_height]/text()}"/>
      <br/><br/>
    </div>
  </xsl:template>

  <xsl:template match="meta[@type='xml/table']">
    <h2><xsl:value-of select="@label"/></h2>
    <xsl:variable name="table_div_id" select="generate-id(.)"/>
    <div id="{$table_div_id}"><span id="loading_{$table_div_id}" style="padding:3px;background-color:darkred;color:white">Loading...</span></div>
    <script type="text/javascript">
      load_table ( "<xsl:value-of select="$table_div_id"/>", "<xsl:value-of select="@id"/>" );
    </script>
  </xsl:template>

  <xsl:template match="meta[@type='text/plain']">
    <xsl:variable name="this_div_id" select="generate-id(.)"/>
    <h2><a onmouseover="this.style.cursor='pointer'" onclick="javascript:document.getElementById('{$this_div_id}').style.display=''"><xsl:value-of select="@label"/></a></h2>
    <div id="{$this_div_id}" style="display:none">
    <iframe width="100%" height="200" src="{text()}"/>
    </div>
  </xsl:template>

  <xsl:template match="meta[@type='number']">
    <table border="1" cellpadding="2" cellspacing="0">
      <tr>
        <td width="200"><xsl:value-of select="@label"/></td><td valign="middle" width="100"><xsl:value-of select="text()"/></td>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="meta[@type='chime']">
    <h2><xsl:value-of select="@label"/></h2>
    <xsl:value-of select="." disable-output-escaping="yes"/>
  </xsl:template>

  <xsl:template match="meta[@type='blank']">
    <span style="color:red;font-weight:bold">No results returned!</span>
  </xsl:template>

  <xsl:template match="meta[@name='html']">
    <script language="javascript">
<![CDATA[
    function
    resize_contentiframe( id )
        {
        f = document.getElementById(id);
        if (f.contentDocument)
            {
            fDocument = f.contentDocument; 
            }
        else if (f.contentWindow)
            { // For IE5.5 and IE6
            fDocument = f.contentWindow.document;
            }
        else if (f.document)
            { // For IE5
            fDocument = f.document;
            }
        else
            {
            return;
            }

        f.height = fDocument.body.scrollHeight + 100;
        f.width = fDocument.body.scrollWidth + 100;
        }
    ]]>
    </script>
    <iframe id="the_contentiframe" onload="resize_contentiframe('the_contentiframe')" width="100%" height="100%" style="border: 1px solid #bfbfbf; overflow: hidden" src="{text()}"/>
  </xsl:template>

  <xsl:template match="meta">
    <xsl:choose>
      <xsl:when test="@label!=''">
        <div style="background-color:#CCCCCC;"><xsl:value-of select="@label"/>: <xsl:value-of select="text()"/></div>
      </xsl:when>
      <xsl:otherwise>
        <div style="background-color:#CCCCCC;"><xsl:value-of select="@name"/>: <xsl:value-of select="text()"/></div>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*"/>

    <xsl:template name="epc_target_links">
        <xsl:if test="$epc_targets">
            <div style="float:right">
            <b>Proceed to:</b>
                <xsl:for-each select="str:tokenize($epc_targets,':')">
                    <xsl:variable name="target_comp_id"    select="str:tokenize(.,',')[1]"/>
                    <xsl:variable name="target_comp_title" select="str:tokenize(.,',')[2]"/>
                    <div style="text-align:left">
                        <a class="follow-assoc" href="{$cgi_href}/ep_ui.pl?render_component_ids={$target_comp_id}">
                            <b>>> <xsl:value-of select="$target_comp_title"/></b>
                        </a>
                    </div>
                </xsl:for-each>
            </div>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
